<?php
/**
 * Created by JetBrains PhpStorm.
 * User: joedarkangel3
 * Date: 8/29/11
 * Time: 5:04 PM
 * To change this template use File | Settings | File Templates.
 */

class item_relation extends CI_Controller {
	/**
	 * load common models and save them to $this without model suffix
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->model('food_category_model', 'food_category');
		$this->load->model('food_option_category_model', 'food_option_category');
        $this->load->model('food_model', 'food');
        $this->load->model('food_option_model', 'food_option');
        $this->load->model('category_relation_model', 'category_relation');
        $this->load->model('item_relation_model', 'item_relation');
        $this->load->library('session');
	}

    function index()
    {
        $food_category = $this->food_category->get_category();
        $food_option_category = $this->food_option_category->get_category();

        // remove option category that is in 'all' scope
        $temp_option_cat = array();
        foreach($food_option_category as $foc) {
            if($foc['scope'] == 'quick')
                $temp_option_cat[] = $foc;
        }
        $food_option_category = $temp_option_cat;
        // -------------------------------------------------

        // get food category selection
        $food_category_select = (int)$this->input->post('food_category_select');
        if($food_category_select == 0){
            $first_category = current($food_category);
            $food_category_select = $first_category['id'];
        }

        // get food item selection
        $food_items = $this->food->get_food($food_category_select);
        $food_select = (int)$this->input->post('food_select');

        if($food_select == 0)
        {
            $first_food = current($food_items);
            $food_select = $first_food['id'];
        }

        // get food option category selection
        $opt_select = (int)$this->input->post('opt_select');
        if($opt_select == 0)
        {
            $first_opt = current($food_option_category);
            $opt_select = $first_opt['id'];
        }

        // get item relation
        $food_option_item = $this->food_option->get_option($opt_select);
        $category_relation = $this->category_relation->read($food_category_select);
        $food_item_relations = array();
        if(!empty($category_relation['food_option_category_id'])){
            $temp_option_items = $this->food_option->get_option($category_relation['food_option_category_id']);
            foreach($temp_option_items as $toi)
                $food_item_relations[] = $toi['id'];
        }

        $food_item_relation_modifiers = $this->item_relation->get_item_relation($food_select);
        // add modifier
        foreach($food_item_relation_modifiers['add'] as $adder){
            $food_item_relations[] = $adder['food_option_id'];
        }
        // remove modifier
        foreach($food_item_relation_modifiers['remove'] as $remover){
            $index = array_search($remover['food_option_id'], $food_item_relations);
            if($index !== false)
                unset($food_item_relations[$index]);
        }
        // populate all data
        $data = array(
            'food_category' => $food_category,
            'food_category_select' => $food_category_select,
            'food_option_category' => $food_option_category,
            'food_items' => $food_items,
            'food_select' => $food_select,
            'opt_select' => $opt_select,
            'food_option_items' => $food_option_item,
            'food_item_relations' => $food_item_relations,
            'page_title' => 'item relation',
            'page_css' => array('admin.css'),
            'page_js' => array()
        );

        $this->load->view('admin/item_relation', $data);
    }

    function change_relation(){
        $food_item_id = $this->input->post('food_item');
        $option_item_id = $this->input->post('option_item');
        $action = $this->input->post('action');

        $this->item_relation->create($food_item_id, $option_item_id, $action);
    }
}